package com.example.order.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.order.entity.Order;
import com.example.order.service.OrderService;
import com.example.utils.JwtUtils;
import com.example.utils.Result;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-21
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@PostMapping("createOrder/{courseId}")
	public Result createOrder(@PathVariable String courseId,HttpServletRequest request) {
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		String orderNo = orderService.saveOrder(courseId,memberId);
		return Result.ok().data("orderNo",orderNo);		
	}
	
	@GetMapping("getOrder/{orderNo}")
	public Result getOrder(@PathVariable String orderNo) {
		QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("order_no", orderNo);
		Order order = orderService.getOne(queryWrapper);
		return Result.ok().data("order",order);		
	}

	@GetMapping("getOrder/status/{courseId}")
	public Result getOrder(@PathVariable String courseId,HttpServletRequest request) {
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		System.out.println(memberId);
		Integer status = orderService.getStatus(courseId,memberId);
		return Result.ok().data("status",status);
	}


}

